package com.mojianxi.ad.index;

import org.springframework.beans.BeansException;
import org.springframework.boot.origin.PropertySourceOrigin;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.core.PriorityOrdered;
import org.springframework.stereotype.Component;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @Description TODO
 * @Author:liuxiaodong
 * @Date 2019/11/6 4:18
 * @Company dyld
 */
@Component
public class DataTable implements ApplicationContextAware, PriorityOrdered {
    private static ApplicationContext applicationContext;
    private static final Map<Class,Object> dataTableMap=new ConcurrentHashMap<>();
    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        DataTable.applicationContext=applicationContext;
    }

    @Override
    public int getOrder() {
        return PriorityOrdered.HIGHEST_PRECEDENCE;
    }
    @SuppressWarnings("all")
    public static <T> T of(Class<T> clazz){
       T instance= (T) dataTableMap.get(clazz);
       if(null!=instance){
           return instance;
       }
       dataTableMap.put(clazz,bean(clazz));
       return (T) dataTableMap.get(clazz);
    }
    @SuppressWarnings("all")
    private static <T> T bean(String beanName){
        return (T)applicationContext.getBean(beanName);
    }
    @SuppressWarnings("all")
    private static <T> T bean(Class clazz){
        return (T)applicationContext.getBean(clazz);
    }
}
